<%@ page language="java" pageEncoding="UTF-8" %>
<%@page import="global.Constants"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
	   //根据主表id查找从表
	   var baseParams = {limit:<%=Constants.PAGE_SIZE %>,start : 0,deptid:0};
	   var EXT_QUERY_TIME_OUT = 1200000;
	   var dListStore = null ;
	   var pidd = '';   //  父id的值
	   var pname = '';   // 被选中的父类名称
      var paraShu = 0;
	
	
	
		$(document).ready(function(){
			// 全局参数
			
			
	
			// 用户数据源
			// 用户数据源
		    var userListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/returnUserInforPagesize.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields: ['id', 'name', 'password', 'remark', 'Login_name', 'mobile', 'email'],
		        autoLoad: false,
				listeners : {  
          load : function() {  
              isDetailReady = true;// 查询置换标志为已查过   
            }  
        
        }  

		    });
			// 用户数据源  结束 
			
			
	
		  
               
               /////////////////////

			
			/// 树
        
        var loader = new Ext.tree.TreeLoader({
			    preloadChildren : true,  
			    baseParams: {parentId:'1'},
			     dataUrl:'${pageContext.request.contextPath}/department_getDepartmentTree.action',
                listeners : {
					beforeload : function(treeLoader, node) {
						this.baseParams.parentId = node.id
					}
				}
                });
               //树面版
			var depttree = new Ext.tree.TreePanel({
                el : "container_dept",
                animate:true,//以动画形式伸展,收缩子节点
                rootVisible:true,//是否显示根节点
		        labelWidth: 50,
		        border: false,
		        bodyStyle: 'background-color:transparent;',
                autoScroll : true,
                autoHeight : true,
                loader: loader,
                width : 350,
                height:400,
                lines : true,
                listeners: {
                click: function(n) {
                    userListStore.load({params:{limit:<%=Constants.PAGE_SIZE %>,start : 0,dept:n.id}});         //  每次取该部门下的用户
              
                  }
                 }
                });

            // 根节点
            var root = new Ext.tree.AsyncTreeNode({
            nodeType: 'async',
            id:'0',
            text:'部门管理树'
            })
            
            depttree.setRootNode(root);
            depttree.render();
 
			// 树结束
			    
			    			// 工具栏  开始
			  
		    var toolbar = new Ext.Toolbar({
		    	 renderTo: 'addDeptToolBarDiv',
		    	 anchor:'60%',
		    	 border:false,
		    	 items: [
				    new Ext.Button({                                    //增加按纽
					    id: 'save-button',
						text: '新增',
						iconCls: 'chart_organisation_add',
						listeners:{                        //添加监听事件 可以结合handler测试这两个事件哪个最先执行
                        "click":function(){

			        var selectedObj = depttree.getSelectionModel().getSelectedNode();  //  找到树节点
			        
			      
			         if(selectedObj == null ){
			         Ext.Msg.alert("警告", "请选择部门节点");
			         return false ;
			         }
			       var selectedId = selectedObj.id;
				   var adddeptFormPanel = new Ext.FormPanel({                    // 增加 面版
					id: 'adddeptFormPanel',
			 		labelWidth: 60,
			         buttonAlign: 'center',
			        border: false,
		    	    bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{// 名称
			                xtype:'textfield',
			                name: 'name',
			                fieldLabel: '部门名称',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20,
			                 listeners:{
			                blur:function(n){
			                var va = n.getValue();
			                Ext.Ajax.request({
			                 
	                        url:  '${pageContext.request.contextPath}/proving_provingOnly.action',
		                     params:{val:va,filed:'dept_name',table:'department'},
		                    success: function(res){
		                      var isPorv = res.responseText;
		                      if(isPorv == 1){
		                        n.setValue('');
		                        Ext.Msg.alert('错误', '不能输入一样的名称');
		                        }
			                }
			                })
			                }
			                }
			           },{// 名称
			                xtype:'textfield',
			                name: 'code',
			                fieldLabel: '部门代码',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           },{   // 父id
			                xtype:'hidden',
			                name: 'pid',
			                fieldLabel: '父id',
			                anchor:'98%',
			                allowBlank:false,
			                value: selectedId,
			                maxLength: 20
			           }],
			        buttons: [{                                               //按纽
	                    text:'提交',
	                    handler: function(){
				        	var form = adddeptFormPanel.getForm();
						    if(form.isValid()){
						    	// 发送请求
								Anynote.ajaxRequest({
								 
									baseParams: form.getValues(),
								  	action: '${pageContext.request.contextPath}/department_addDepartment.action?lay=0',
									callback: function(jsonResult){
									  	addDeptWindowd.close();
							     	    root.reload(function(){});
										 
								 
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                       adddeptFormPanel.getForm().reset();
	                    }
	                }]                                                       //按纽结束
			    });
		
				
			var addDeptWindowd = new Ext.Window({                            // 新增窗口
			       iconCls: 'chart_organisation_add',
				   title: '添加部门',
				   width: 330,
					height: 170,
					modal: true,
					maximizable: false,
					resizable: false,
					layout:'fit',
					plain: true,
				listeners: {
					close: function(){

					}
				},
				items: [adddeptFormPanel]
			}).show();                                                       // 新增窗口 结束
                 ////////////////////////////// 
                          }
                        }
					}),                                         // //增加按纽  结束
					'-',
					/////////////////
					new Ext.Button({                               // 新增 修改 按纽
					    id: 'update-button',
						text: '修改',
						iconCls: 'chart_organisation',
					   labelWidth: 60,
			          buttonAlign: 'center',
			          border: false,
			      
			        url: '',
						listeners:{  //添加监听事件 可以结合handler测试这两个事件哪个最先执行
                        "click":function(){                                //  点击事件 开始
                        
                        var updeptFormPanel = new Ext.FormPanel({     // 修改 面版 开始
					 labelWidth: 60,
			         buttonAlign: 'center',
			         border: false,
		    	     bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			         
		            items:[{// 名称
			                xtype:'textfield',
			                name: 'name',
			                fieldLabel: '部门名称',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20,
			                 listeners:{            //判断重复记录
			                blur:function(n){
			                var va = n.getValue();
			                Ext.Ajax.request({
			                 
	                        url:  '${pageContext.request.contextPath}/proving_provingOnly.action',
		                     params:{val:va,filed:'dept_name',table:'department'},
		                    success: function(res){
		                      var isPorv = res.responseText;
		                      if(isPorv == 1){
		                        n.setValue('');
		                        Ext.Msg.alert('错误', '不能输入一样的名称');
		                        }
			                }
			                })
			                }
			                }
			           },{// 名称
			                xtype:'textfield',
			                name: 'code',
			                fieldLabel: '部门代码',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           },{   // 父id
			                xtype:'hidden',
			                name: 'id',
			                fieldLabel: 'id',
			                anchor:'98%',
			                allowBlank:false,
			                value: selectedId,
			                maxLength: 20
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
				        	var form = updeptFormPanel.getForm();
						    if(form.isValid()){
						    	// 发送请求
								Anynote.ajaxRequest({
								 
									baseParams: form.getValues(),
								  	action: '${pageContext.request.contextPath}/department_updateDictionary.action?lay=0',
									callback: function(jsonResult){
									     
									  	upDeptWindowd.close();
							     	    root.reload(function(){});
										 
								 
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		   updeptFormPanel.getForm().reset();
	                    }
	                }]
			    });                                          // 修改面版 结束
                        
                          // 开始执行 修改 过程
                        var selectedObj = depttree.getSelectionModel().getSelectedNode();
			             if(selectedObj == null ){
			               Ext.Msg.alert("警告", "请选择部门节点");
			               return false ;
			             }
			               var selectedId = selectedObj.id;
                        
                          Anynote.ajaxRequest({                     // 修改发送 后台              
                        baseParams: {id:selectedId},
                        action: '${pageContext.request.contextPath}/department_findDepartment.action',
				         callback: function(jsonResult){
				        var da = jsonResult.datas;
				        var currentData = da[0];
				   
			 	      	updeptFormPanel.getForm().setValues(currentData);
			          	}
			           });
                        
                        
                      			       
				
				// 修改部门窗口
				var upDeptWindowd = new Ext.Window({
				    iconCls: 'chart_organisation',
					title: '修改当前部门',
					width: 330,
					height: 170,
					modal: true,
					maximizable: false,
					resizable: false,
					layout:'fit',
					plain: true,
					items: [updeptFormPanel]
				}).show();
             
                          }           //  点击修改事件结束
                        }                    //  监听事件结束
					}),                            //  修改按纽结束
					'-',
				
					/////////////////              // 删除按纽开始
					new Ext.Button({
					    id: 'delete-button',
						text: '删除',
						iconCls: 'chart_organisation_delete',
						listeners:{//添加监听事件 可以结合handler测试这两个事件哪个最先执行
                        "click":function(){                                    // 点击事件开始
                       var selectedObj = depttree.getSelectionModel().getSelectedNode();
			            if(selectedObj == null ){
			              Ext.Msg.alert("警告", "请选择部门节点");
			            return false ;
			         }   
			       var selectedId = selectedObj.id;
			       //     if(form.isValid()){
			             // 发送请求
					   Anynote.ajaxRequest({
					    baseParams: {id:selectedId},
						action: '${pageContext.request.contextPath}/department_deleteDepartment.action',
						callback: function(jsonResult){
						    
						    root.reload(function(){});
						 },
						showWaiting: true,
						failureMsg: '保存失败.'
					  });
				 
                
                          }         //   删除 点击事件结束            
                        }
					})       // //   删除按纽  结束
				      ]              // 按纽结束
			});               // 工具栏  结束
			    
			    
			    		
            
            //////////////////  用户 grid
            
                // 用户数据表格
				
			var d_userListGrid = new Ext.grid.GridPanel({
				id: 'd_userListGrid',
				//  renderTo: 'userListGridDiv',
				border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: userListStore,
		        loadMask: true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [
						 
						new Ext.grid.RowNumberer({header:'№'}),
						{id:'id',header: '用户ID', width: 100, hidden: true, sortable: true, dataIndex: 'id'},
			            {id:'name',header: '用户姓名', width: 100, sortable: true, dataIndex: 'name'},
			            {id:'Login_name',header: '登录账号', width: 100, sortable: true, dataIndex: 'Login_name'},
			            {id:'mobile',header: '用户手机号', width: 100, sortable: true, dataIndex: 'mobile'},
			            {id:'email',header: '用户Email', width: 180, sortable: true, dataIndex: 'email'}
					]
		        }),
		      
		        columnLines: true,
		       bbar: new Ext.PagingToolbar({
					pageSize: <%=Constants.PAGE_SIZE %>,
					store: userListStore,
					displayInfo: true,
					displayMsg: Anynote.PAGINGTOOLBAR_DISPLAYMSG,
					emptyMsg: Anynote.PAGINGTOOLBAR_EMPTYMSG,
					doLoad: function(start){
					    baseParams.start = start;
					    var selectedObj = depttree.getSelectionModel().getSelectedNode();
					     alert(selectedObj.id);
						this.store.load({params: baseParams});
					}
		        })
		    });
		    // 用户数据表格  结束

			
		   	    // 工具栏  开始
		   
               // 左面版
			var deptPanel = new Ext.Panel({
				id: 'deptPanel',
				region: 'west',
				border: false,
				bodyStyle: 'border-right-width:1px;',
				split: true,
                width: 300,
                minSize: 200,
                maxSize: 500,
				layout:'fit',
				 height:460,
				 autoScroll:true,
                collapseMode:'mini',
				items: [depttree]

			});
			
			  //右面版
			   var userPanel = new Ext.Panel({
				id: 'userPanel',
				region: 'center',
				border: false,
				bodyStyle: 'border-left-width:1px;',
				layout:'fit',
                collapseMode:'mini',
				items: [d_userListGrid]
				
			});

			// 主面板
			var departmentMainPanel = new Ext.Panel({
				id: 'departmentMainPanel',
				renderTo: 'departmentMainPanelDiv',
				border: false,
				layout: 'border',

				items: [deptPanel,userPanel]
			});
	  	     Anynote.resizeGridTo("departmentMainPanel", 0,56);
                    // 工具栏  结束
			
			//////////////////////////////////////////////////////////////////结束
			
			
		
			
			
			
});

      
       
       
       
          
	</script>
</head>
<body>
<div id="addDeptToolBarDiv"></div>
<div id="departmentMainPanelDiv" style="width:100%;height:100%;"></div>



<div id="container_dept"></div>


</body>
</html>
